package ioc

import (
	"gitee.com/youkelike/zlog"
	grpc2 "gitee.com/youkelike/zmigrate/example/interactive/grpc"
	"gitee.com/youkelike/zmigrate/pkg/grpcx"
	"github.com/spf13/viper"
	clientv3 "go.etcd.io/etcd/client/v3"
	"google.golang.org/grpc"
)

func InitGRPCxServer(
	l zlog.Logger,
	intrServer *grpc2.InteractiveServiceServer) *grpcx.Server {
	type Config struct {
		Port      int      `yaml:"port"`
		EtcdAddrs []string `yaml:"etcdAddrs"`
	}

	var cfg Config
	err := viper.UnmarshalKey("grpc.server", &cfg)
	// master 分支
	//err := viper.UnmarshalKey("grpc", &cfg)
	if err != nil {
		panic(err)
	}

	cli, err := clientv3.New(clientv3.Config{
		Endpoints: cfg.EtcdAddrs,
	})
	if err != nil {
		panic(err)
	}

	server := grpc.NewServer()
	intrServer.Register(server)

	return &grpcx.Server{
		Server:     server,
		Port:       cfg.Port,
		EtcdClient: cli,
		EtcdTTL:    600,
		Name:       "interactive",
		L:          l,
	}
}
